﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Fcom.VNInsurance.Web.Controllers
{
    public class CompanyProductController : CultureHelper.CultureController
    {
        //
        // GET: /CompanyProduct/
        Models.VNInsuranceDataContext db = new Models.VNInsuranceDataContext();
        public ActionResult Index()
        {
            return View();
        }

        //CompanyProduct
        public ActionResult CompanyProductDetail(int ID)
        {
            var companyproductlang = db.CompanyProductLangs.Where(m => m.CProductID == ID && m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
            var LangProduct = db.ProductLangs.Where(p => p.ProductID == companyproductlang.CompanyProduct.ProductID && p.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
            ViewBag.LangProduct = LangProduct;
            var LangCatProduct = db.ProductCatLangs.Where(p => p.CatID == LangProduct.Product.ProductCat.ID && p.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
            ViewBag.LangCat = LangCatProduct;
            bool showgara = false;
            bool showhopital = false;

            if (LangCatProduct.CatID == 2)
            { showhopital = true; }
            if (LangCatProduct.CatID == 3)
            { showgara = true; }
            var LangCPProduct = (from cp in db.CompanyProductLangs
                                 where cp.LangID == Request.Cookies["_culture"]["lang"] && cp.CProductID == ID && cp.CompanyProduct.Enable == true && cp.CompanyProduct.Company.Enable == true
                                 select new Fcom.VNInsurance.Web.Models.CPInfo() { showGaras = showgara, showHopitals = showhopital, Condition = cp.Condition, ProductImage = cp.ProductImage, CompanyID = cp.CompanyProduct.CompanyID, Intro = cp.Intro, CProductID = cp.CProductID, ProductName = cp.DisplayName, CompanyName = cp.CompanyProduct.Company.CompanyLangs.Where(n => n.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault().CompanyName, CompanyLogo = cp.CompanyProduct.Company.CompanyLangs.Where(n => n.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault().CompanyLogo, Benefit = cp.Benefit, Future = cp.Future, MoreInfo = cp.Moreinfo, Fare = cp.OneFare, InsuranceMoney = cp.InsuranceMoney }).FirstOrDefault();
            if (LangCPProduct == null)
            {
                return RedirectToAction(LangProduct.Product.Action, LangProduct.Product.Controller);
            }

            var OtherLangCPProduct = from cp in db.CompanyProductLangs
                                     where cp.LangID == Request.Cookies["_culture"]["lang"] && cp.CProductID != ID && cp.CompanyProduct.CompanyID == companyproductlang.CompanyProduct.CompanyID && cp.CompanyProduct.ProductID == LangProduct.ProductID && cp.CompanyProduct.Enable == true && cp.CompanyProduct.Company.Enable == true
                                     select new Fcom.VNInsurance.Web.Models.CPInfo() { showGaras = showgara, showHopitals = showhopital, Condition = cp.Condition, ProductImage = cp.ProductImage, CompanyID = cp.CompanyProduct.CompanyID, Intro = cp.Intro, CProductID = cp.CProductID, ProductName = cp.DisplayName, CompanyName = cp.CompanyProduct.Company.CompanyLangs.Where(n => n.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault().CompanyName, CompanyLogo = cp.CompanyProduct.Company.CompanyLangs.Where(n => n.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault().CompanyLogo, Benefit = cp.Benefit, Future = cp.Future, MoreInfo = cp.Moreinfo, Fare = cp.OneFare, InsuranceMoney = cp.InsuranceMoney };

            ViewBag.CurrentLang = LangProduct.Language;
            ViewBag.OtherLangCPProduct = OtherLangCPProduct;
            return View(LangCPProduct);

        }
        [Authorize]
        public ActionResult Apply(int ID)
        {
            var companyproductlang = db.CompanyProductLangs.Where(m => m.CProductID == ID && m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
            if (!User.Identity.IsAuthenticated||companyproductlang==null)
            {
                return RedirectToAction(companyproductlang.CompanyProduct.Product.Action, companyproductlang.CompanyProduct.Product.Controller);
            }
            Fcom.IDLib.SaveMoneyID.AccountsynClient client = new IDLib.SaveMoneyID.AccountsynClient();
            Fcom.IDLib.SaveMoneyID.FullUserInfo userinfo = client.getUserInfo(User.Identity.Name);
            Fcom.VNInsurance.Web.Models.Policy policy = new Models.Policy();
            policy.Address = userinfo.Address;
            policy.CProductID = ID;
            policy.Email = userinfo.Email;
            policy.username = userinfo.Username;
            policy.PolicyDate = DateTime.Now;
            policy.isContacted = false;
            policy.FullName = userinfo.Fullname;
            policy.Phone = userinfo.Phone;
            db.Policies.InsertOnSubmit(policy);
            db.SubmitChanges();
            client.Close();

            var thanks = db.NewsLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"] && m.New.NCatID == 7).FirstOrDefault();
            Fcom.VNInsurance.Web.Models.SupportNew spnews = new Models.SupportNew();
            spnews.Email = policy.Email;
            spnews.FullContent = "yêu cầu phí sản phẩm[" + companyproductlang.DisplayName + "]<br><br>Họ và tên:"+policy.FullName+"<br> Username:" + policy.username + "<br>Email:" + policy.Email + "<br>Phone:" + policy.Phone+"<br>Địa chỉ:"+policy.Address;
            spnews.RequireteamID = 1;
            spnews.Title = "yêu cầu phí sản phẩm["+companyproductlang.DisplayName+"]"+policy.PolicyDate.ToString("dd/MM/yyyy");
            var smtpconfig = db.ConfigSMTPs.FirstOrDefault();
            var supportteam = db.SupportTeams.Where(t => t.ID == 1).FirstOrDefault();
            var supportters = supportteam.Supporters;
            db.SupportNews.InsertOnSubmit(spnews);
            db.SubmitChanges();
            foreach (var spter in supportters)
            {
                Fcom.VNInsurance.Web.Infrastructure.SMTPSendMail.SendOneEmail(smtpconfig, spter.Email,spnews.Title ,spnews.FullContent+"<br><br><br>Để hỗ trợ vui lòng click vào link này: " + Request.Url.GetLeftPart(UriPartial.Authority) + Url.Action("Answer", "Support", new { SupportContentID = spnews.ID }), true);

            }
            Fcom.VNInsurance.Web.Infrastructure.SMTPSendMail.SendOneEmail(smtpconfig, policy.Email, spnews.Title, spnews.FullContent+thanks.Title+HttpUtility.HtmlDecode(thanks.FullContent), true);
            return RedirectToAction("ThanksForApply");
        }
          [Authorize]
        public ActionResult ThanksForApply()
        {
            var apply = db.NewsLangs.Where(n => n.New.NCatID == 7 && n.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();

            return View(apply);
        }
          public ActionResult CoverageCProductIDMN(int ID)
          {
              var CProduct = db.CompanyProductLangs.Where(m => m.CProductID == ID && m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
              ViewBag.CProduct = CProduct;
              ViewBag.companyLang = CProduct.CompanyProduct.Company.CompanyLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
              ViewBag.coveragelist = new SelectList(db.Coverages,"ID","Name");
              var coverage =( from c in db.CoverageBaseCompanyProducts where c.CProductID == ID select c.Coverage).FirstOrDefault();
              return View(coverage);
          }
        [HttpPost]
          public ActionResult CoverageCProductIDMN(int CProductID, Fcom.VNInsurance.Web.Models.Coverage model)
          {
           
              Fcom.VNInsurance.Web.Models.CoverageBaseCompanyProduct bf = new Models.CoverageBaseCompanyProduct();
              bf.CoverageID = model.ID;
              bf.CProductID = CProductID;
              db.CoverageBaseCompanyProducts.InsertOnSubmit(bf);
              db.SubmitChanges();
            

              var benefits = db.BenefitLangs.Where(m => m.Benefit.CoverageID == model.ID && m.LangID == Request.Cookies["_culture"]["lang"]);
              Models.CompanyProductBenefit bene,bene2;
              foreach (var be in benefits)
              {
                  bene = new Models.CompanyProductBenefit();
                  bene.BenefitID = be.BenefitID;
                  bene.CProductId = CProductID;
                  bene.LangID = "vi-VN";
                  bene.InsuanceFare = 0;
                  bene.InsuanceMoney = 0;
                  db.CompanyProductBenefits.InsertOnSubmit(bene);
                  bene2 = new Models.CompanyProductBenefit();
                  bene2.BenefitID = be.BenefitID;
                  bene2.CProductId = CProductID;
                  bene2.LangID = "en-US";
                  bene2.InsuanceFare = 0;
                  bene2.InsuanceMoney = 0;
                  db.CompanyProductBenefits.InsertOnSubmit(bene2);
                  db.SubmitChanges();
              }
              return RedirectToAction("CoverageCProductIDMN", new { ID=CProductID});
          }
        public ActionResult getBenefitCP(int ID)
        { 
            //
            var benes = from b in db.CompanyProductBenefits where b.LangID == Request.Cookies["_culture"]["lang"] && b.CProductId == ID select new {CProductID= b.CProductId,b.BenefitID,b.InsuanceFare,b.InsuanceMoney,b.Benefit.BenefitLangs.Where(n=>n.LangID==Request.Cookies["_culture"]["lang"]).FirstOrDefault().DisplayName,b.MotobikeCCFare,b.MotoDownInsuranceFare,b.MotoUpInsuranceFare,b.RateBenefit,b.Rate,b.InsmoneyByPrice};
            return Json(benes,JsonRequestBehavior.AllowGet);
        }
        [Authorize]
        public ActionResult Compare(int ID)
        {
            bool showgara = false;
            bool showhopital = false;
            var LangProduct = db.ProductLangs.Where(p => p.ProductID == ID && p.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
            ViewBag.LangProduct = LangProduct;
            var LangCatProduct = db.ProductCatLangs.Where(p => p.CatID == LangProduct.Product.ProductCat.ID && p.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
            if (LangCatProduct.CatID == 2)
            { showhopital=true;}
            if (LangCatProduct.CatID == 3)
            { showgara = true; }

            ViewBag.LangCat = LangCatProduct;
            ViewBag.productFAQ = db.ProductFAQs.Where(p => p.LangID == Request.Cookies["_culture"]["lang"] && p.ProductID == ID);
            var LangCPProduct = from cp in db.CompanyProductLangs
                                where cp.LangID == Request.Cookies["_culture"]["lang"] && cp.CompanyProduct.ProductID == ID && cp.CompanyProduct.Enable == true && cp.CompanyProduct.Company.Enable == true
                                select new Fcom.VNInsurance.Web.Models.CPInfo() {showGaras=showgara,showHopitals=showhopital, Condition = cp.Condition, ProductImage = cp.ProductImage, CompanyID = cp.CompanyProduct.CompanyID, CProductID = cp.CProductID, ProductName = cp.DisplayName, CompanyName = cp.CompanyProduct.Company.CompanyLangs.Where(n => n.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault().CompanyName, CompanyLogo = cp.CompanyProduct.Company.CompanyLangs.Where(n => n.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault().CompanyLogo, Benefit = cp.Benefit, Future = cp.Future, MoreInfo = cp.Moreinfo, Fare = cp.OneFare, InsuranceMoney = cp.InsuranceMoney };
            ViewBag.CurrentLang = LangProduct.Language;
            return View(LangCPProduct);

        }
        [Authorize(Roles = "13,14")]
        public ActionResult ManageProductByID(int ID)
        {
            var baseproduct = db.ProductLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"] && m.ProductID == ID).FirstOrDefault();

            return View(baseproduct);
        }
        //
         public ActionResult getJsonCPProductPaper(int ID)
         {
             var cpdocs = from m in db.CompanyProductPapers where m.CProductID == ID select new { m.ID, m.imageName };
             return Json(cpdocs, JsonRequestBehavior.AllowGet);
         }
         //editCompanyProductDocs
         public ActionResult editCompanyProductPaper(int ID)
         {
             var cpdocs = db.CompanyProductPapers.Where(m => m.ID == ID).FirstOrDefault();
             ViewBag.companyproductlang = db.CompanyProductLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"] && m.CProductID == cpdocs.CProductID).FirstOrDefault();

             return View(cpdocs);
         }
         [HttpPost]
         public ActionResult editCompanyProductPaper(Models.CompanyProductPaper model)
         {
             if (ModelState.IsValid)
             {
                 var cpdocs = db.CompanyProductPapers.Where(m => m.ID == model.ID).FirstOrDefault();
                 cpdocs.imageName = model.imageName;
   
                 db.SubmitChanges();
                 return RedirectToAction("CompanyProductPapers", new { ID = model.CProductID });
             }
             return View();
         }
         public ActionResult DeleteCPPapersID(int ID)
         {
             var docs = db.CompanyProductPapers.Where(m => m.ID == ID).FirstOrDefault();
             db.CompanyProductPapers.DeleteOnSubmit(docs);
             db.SubmitChanges();
             return Json(new { success = true }, JsonRequestBehavior.AllowGet);
         }

         public ActionResult addCompanyProductPapers(int ID)
         {
             ViewBag.companyproductlang = db.CompanyProductLangs.Where(m => m.CProductID == ID).FirstOrDefault();
             return View();
         }
         [HttpPost]
         public ActionResult addCompanyProductPapers( Fcom.VNInsurance.Web.Models.CompanyProductPaper model)
         {
             if (ModelState.IsValid)
             {
                 Models.CompanyProductPaper paper = new Models.CompanyProductPaper();
                 paper.CProductID = model.CProductID;
                 paper.imageName = model.imageName;
                 db.CompanyProductPapers.InsertOnSubmit(paper);
                 db.SubmitChanges();
                 return RedirectToAction("CompanyProductPapers", new { ID = model.CProductID });
             }
             return View();
         }
         public ActionResult CompanyProductPapers(int ID)
         {
             var companyproductlang = db.CompanyProductLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"] && m.CProductID == ID).FirstOrDefault();
             return View(companyproductlang);
         }
        //
         public ActionResult getJsonCPProductDocs(int ID)
         {
             var cpdocs = from m in db.CompanyProductDocs where m.CProductID == ID && m.LangID == Request.Cookies["_culture"]["lang"] select new { m.ID,m.Title,m.Value,m.istype};
             return Json(cpdocs,JsonRequestBehavior.AllowGet);
         }
         //editCompanyProductDocs
         public ActionResult editCompanyProductDocs(int ID)
         {
             var cpdocs = db.CompanyProductDocs.Where(m => m.ID == ID).FirstOrDefault();
             ViewBag.companyproductlang = db.CompanyProductLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"] && m.CProductID == cpdocs.CProductID).FirstOrDefault();
            
             return View(cpdocs);
         }
         [HttpPost]
         public ActionResult editCompanyProductDocs(Models.CompanyProductDoc model)
         {
             if (ModelState.IsValid)
             {
                 var cpdocs = db.CompanyProductDocs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]&&m.ID==model.ID).FirstOrDefault();
                 cpdocs.Title = model.Title;
                 cpdocs.Value = model.Value;
                 db.SubmitChanges();
                 return RedirectToAction("CompanyProductDocs", new { ID = model.CProductID });
             }
             return View();
         }
         public ActionResult DeleteCPDocsID(int ID)
         {
             var docs = db.CompanyProductDocs.Where(m => m.ID == ID && m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
             db.CompanyProductDocs.DeleteOnSubmit(docs);
             db.SubmitChanges();
             return Json(new { success = true }, JsonRequestBehavior.AllowGet);
         }

         public ActionResult addCompanyProductDocs(int ID)
         {
            ViewBag.companyproductlang = db.CompanyProductLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"] && m.CProductID == ID).FirstOrDefault();
             return View();
         }
        [HttpPost]
         public ActionResult addCompanyProductDocs(int istytpe, Fcom.VNInsurance.Web.Models.CompanyProductDoc model)
         {
             if (ModelState.IsValid)
             {
                 Models.CompanyProductDoc doc = new Models.CompanyProductDoc();
                 doc.Value = model.Value;
                 doc.istype = istytpe;
                 doc.Title = model.Title;
                 doc.LangID = Request.Cookies["_culture"]["lang"];
                 doc.CProductID = model.CProductID;
                 db.CompanyProductDocs.InsertOnSubmit(doc);
                 
                 db.SubmitChanges();
                 return RedirectToAction("CompanyProductDocs", new { ID=model.CProductID});
             }
             return View();
         }
         public ActionResult CompanyProductDocs(int ID)
         {
             var companyproductlang = db.CompanyProductLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]&&m.CProductID==ID).FirstOrDefault();
             return View(companyproductlang);
         }
        public ActionResult getJsonCPProduct(int ID)
        {
            var products = from m in db.CompanyProductLangs
                           where m.LangID == Request.Cookies["_culture"]["lang"] && m.CompanyProduct.ProductID == ID
                           select new { m.CProductID,m.DisplayName,m.CompanyProduct.Company.CompanyLangs.Where(n=>n.LangID==Request.Cookies["_culture"]["lang"]).FirstOrDefault().CompanyLogo,m.OneFare,m.InsuranceMoney};
            return Json(products, JsonRequestBehavior.AllowGet);

        }
         [Authorize(Roles = "13,14")]
        public ActionResult EditCompanyProduct(int ID)
        {
            var companyproduct = db.CompanyProductLangs.Where(m => m.CProductID == ID && Request.Cookies["_culture"]["lang"]==m.LangID).FirstOrDefault();
           ViewBag.CompanyInfo = companyproduct.CompanyProduct.Company.CompanyLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
           companyproduct.Benefit = HttpUtility.HtmlDecode(companyproduct.Benefit);
           companyproduct.Intro = HttpUtility.HtmlDecode(companyproduct.Intro);
           companyproduct.Moreinfo = HttpUtility.HtmlDecode(companyproduct.Moreinfo);
           companyproduct.Condition = HttpUtility.HtmlDecode(companyproduct.Condition);
         
           ViewBag.Lang = db.Languages.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
           return View(companyproduct);
        }
        [HttpPost,ValidateInput(false)]
        public ActionResult EditCompanyProduct(bool  Enable,Models.CompanyProductLang model)
        {
            if (ModelState.IsValid)
            {
                var companyproduct = db.CompanyProductLangs.Where(m => m.CProductID == model.CProductID && m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
                companyproduct.Benefit = HttpUtility.HtmlEncode(model.Benefit);
                companyproduct.DisplayName = model.DisplayName;
                companyproduct.Intro = HttpUtility.HtmlEncode(model.Intro);
                companyproduct.Moreinfo = HttpUtility.HtmlEncode(model.Moreinfo);
                companyproduct.Condition = HttpUtility.HtmlEncode(model.Condition);
                companyproduct.OneFare = model.OneFare;
                companyproduct.InsuranceMoney = model.InsuranceMoney;
                companyproduct.CompanyProduct.Enable = Enable;
                companyproduct.Paytem = model.Paytem;
                companyproduct.Guaranteed = model.Guaranteed;
                companyproduct.Expected = model.Expected;
                companyproduct.Premiumtopay = model.Premiumtopay;
                companyproduct.ProductImage = model.ProductImage;
                db.SubmitChanges();
                return RedirectToAction("ManageProductByID", new { ID=companyproduct.CompanyProduct.ProductID});

            }
            return View();
        }
        //them
         [Authorize(Roles="13,14")]
        public ActionResult DeleteCPID(int ID)
        {
            var companyproductlang = db.CompanyProductLangs.Where(m => m.CProductID == ID);
            db.CompanyProductLangs.DeleteAllOnSubmit(companyproductlang);
            db.SubmitChanges();
            var cp = db.CompanyProducts.Where(m => m.ID == ID).FirstOrDefault();
            db.CompanyProducts.DeleteOnSubmit(cp);
            db.SubmitChanges();
            return Json(new { success=true});

        }
         [Authorize(Roles = "13,14")]
        public ActionResult AddCompanyProduct(int ID)
        {
            ViewBag.productlang = db.ProductLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"] && m.ProductID == ID).FirstOrDefault();
            var CompanyListLang = db.CompanyLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]);
            ViewBag.CompanyListLang = new SelectList(CompanyListLang,"CompanyID","CompanyName");
            ViewBag.Lang = db.Languages.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
            return View();
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult AddCompanyProduct(bool Enable,int ProductID,int CompanyID,Models.CompanyProductLang model)
        {
            if (ModelState.IsValid)
            {
                Models.CompanyProduct comproduct = new Models.CompanyProduct();
                comproduct.CompanyID = CompanyID;
                comproduct.CPName = model.DisplayName;
                comproduct.ProductID = ProductID;
                comproduct.Enable = Enable;
                db.CompanyProducts.InsertOnSubmit(comproduct);
                db.SubmitChanges();
           
                Models.CompanyProductLang companyproduct = new Models.CompanyProductLang();
                companyproduct.CProductID = comproduct.ID;
                companyproduct.LangID = "vi-VN";
                companyproduct.Benefit = HttpUtility.HtmlEncode(model.Benefit);
                companyproduct.DisplayName = model.DisplayName;
                companyproduct.Intro = HttpUtility.HtmlEncode(model.Intro);
                companyproduct.Moreinfo = HttpUtility.HtmlEncode(model.Moreinfo);
                companyproduct.Condition = HttpUtility.HtmlEncode(model.Condition);
                companyproduct.OneFare = model.OneFare;
                companyproduct.InsuranceMoney = model.InsuranceMoney;
              
                companyproduct.Paytem = model.Paytem;
                companyproduct.Guaranteed = model.Guaranteed;
                companyproduct.Expected = model.Expected;
                companyproduct.Premiumtopay = model.Premiumtopay;
                companyproduct.ProductImage = model.ProductImage;
                db.CompanyProductLangs.InsertOnSubmit(companyproduct);//

                Models.CompanyProductLang companyproduct2 = new Models.CompanyProductLang();
                companyproduct2.CProductID = comproduct.ID;
                companyproduct2.LangID = "en-US";
                companyproduct2.Benefit = HttpUtility.HtmlEncode(model.Benefit);
                companyproduct2.DisplayName = model.DisplayName;
                companyproduct2.Intro = HttpUtility.HtmlEncode(model.Intro);
                companyproduct2.Moreinfo = HttpUtility.HtmlEncode(model.Moreinfo);
                companyproduct2.Condition = HttpUtility.HtmlEncode(model.Condition);
                companyproduct2.OneFare = model.OneFare;
                companyproduct2.InsuranceMoney = model.InsuranceMoney;
               
                companyproduct2.Paytem = model.Paytem;
                companyproduct2.Guaranteed = model.Guaranteed;
                companyproduct2.Expected = model.Expected;
                companyproduct2.Premiumtopay = model.Premiumtopay;
                companyproduct2.ProductImage = model.ProductImage;
                db.CompanyProductLangs.InsertOnSubmit(companyproduct2);//
                db.SubmitChanges();
                return RedirectToAction("ManageProductByID", new { ID = companyproduct.CompanyProduct.ProductID });
            }
            return View();
        }
        //getBenefitFareList
        public ActionResult EditBenefitFare(int companyid, int benefitid,int companyproduct)
        {
            //company productid
            ViewBag.companylang = db.CompanyLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"] && m.CompanyID == companyid).FirstOrDefault();
            ViewBag.benefitlang = db.BenefitLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"] && m.BenefitID==benefitid).FirstOrDefault();
            ViewBag.companyproduct = db.CompanyProductLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]&&m.CProductID==companyproduct).FirstOrDefault();
            var benefitfare = db.CompanyProductBenefits.Where(m => m.BenefitID == benefitid && m.CProductId == companyproduct && m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
            return View(benefitfare);
        }
        [HttpPost]
        public ActionResult EditBenefitFare(int CompanyProductID,Fcom.VNInsurance.Web.Models.CompanyProductBenefit model)
        {
            var companyproduct = db.CompanyProductBenefits.Where(m => m.BenefitID == model.BenefitID && m.CProductId == CompanyProductID && m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
            if (companyproduct != null)
            {
                companyproduct.InsuanceFare = model.InsuanceFare;
                companyproduct.InsuanceMoney = model.InsuanceMoney;
                companyproduct.MotobikeCCFare = model.MotobikeCCFare;
                companyproduct.MotoDownInsuranceFare = model.MotoDownInsuranceFare;
                companyproduct.MotoUpInsuranceFare = model.MotoUpInsuranceFare;
                companyproduct.RateBenefit = model.RateBenefit;
                companyproduct.Rate = model.Rate;
                companyproduct.InsmoneyByPrice = model.InsmoneyByPrice;
                db.SubmitChanges();
                return RedirectToAction("CoverageCProductIDMN", new { ID = CompanyProductID });
            }
            else
            {
                Models.CompanyProductBenefit cp = new Models.CompanyProductBenefit();
                cp.CProductId = CompanyProductID;
                cp.LangID = Request.Cookies["_culture"]["lang"];
                cp.BenefitID = model.BenefitID;
                cp.InsuanceFare = model.InsuanceFare;
                cp.InsuanceMoney = model.InsuanceMoney;
                cp.MotobikeCCFare = model.MotobikeCCFare;
                cp.MotoDownInsuranceFare = model.MotoDownInsuranceFare;
                cp.MotoUpInsuranceFare = model.MotoUpInsuranceFare;
                cp.RateBenefit = model.RateBenefit;
                cp.Rate = model.Rate;
                cp.InsmoneyByPrice = model.InsmoneyByPrice;
                db.CompanyProductBenefits.InsertOnSubmit(cp);
                db.SubmitChanges();
                return RedirectToAction("CoverageCProductIDMN", new { ID = CompanyProductID });
            }
         
        }
        [Authorize(Roles = "14")]
        public ActionResult getBenefitCompanyProductList(int CoverageID,int CProductID)
        {

            var benlang = db.BenefitLangs.Where(m => ((m.LangID == Request.Cookies["_culture"]["lang"]) && (m.Benefit.CoverageID == CoverageID))).Select(m => new
            {
                BenefitID = m.BenefitID,
                LangID = m.LangID,
                BenefitCoverageName = m.Benefit.Coverage.Name,
                BenefitDisplay = m.DisplayName,
                BenefitEnadbleSub = m.EnableSub,
                BenefitEnableIsmoney = m.EnableInsMoney,
                m.EnableChildBenefit,
                m.EnableNumJoin,
                m.EnableText

            });
            return Json(benlang, JsonRequestBehavior.AllowGet);
        }
        public ActionResult AddCPBenefit(int ID)
        {
           var cp = db.CompanyProductLangs.Where(m =>m.CProductID==ID&& m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
           ViewBag.companyProduct = cp;
           ViewBag.company = cp.CompanyProduct.Company.CompanyLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
           var Coverage = db.Coverages;
           ViewBag.Coveragelist = new SelectList(Coverage, "ID", "Name");
           var benefitlang = db.BenefitLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]&&m.Benefit.CoverageID==Coverage.FirstOrDefault().ID);

           var benefitincp = from m in db.CompanyProductBenefits where m.CProductId == ID && m.LangID == Request.Cookies["_culture"]["lang"] select m.Benefit.BenefitLangs.Where(n => n.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
           var benefitinlang = benefitlang.Except(benefitincp);
           ViewBag.benefitlist = new SelectList(benefitinlang, "BenefitID", "DisplayName");
           return View();
        }
        [HttpPost]
        public ActionResult AddCPBenefit(Fcom.VNInsurance.Web.Models.CompanyProductBenefit model)
        {
            Models.CompanyProductBenefit cp = new Models.CompanyProductBenefit();
            cp.CProductId = model.CProductId;
            cp.BenefitID = model.BenefitID;
            cp.LangID ="vi-VN";
            cp.InsuanceFare = model.InsuanceFare;
            cp.InsuanceMoney = model.InsuanceMoney;
            cp.MotobikeCCFare = model.MotobikeCCFare;
            cp.MotoDownInsuranceFare = model.MotoDownInsuranceFare;
            cp.MotoUpInsuranceFare = model.MotoUpInsuranceFare;
            db.CompanyProductBenefits.InsertOnSubmit(cp);

            Models.CompanyProductBenefit cp2 = new Models.CompanyProductBenefit();
            cp2.CProductId = model.CProductId;
            cp2.BenefitID = model.BenefitID;
            cp2.LangID = "en-US";
            cp2.InsuanceFare = model.InsuanceFare;
            cp2.InsuanceMoney = model.InsuanceMoney;
            cp2.MotobikeCCFare = model.MotobikeCCFare;
            cp2.MotoDownInsuranceFare = model.MotoDownInsuranceFare;
            cp2.MotoUpInsuranceFare = model.MotoUpInsuranceFare;
            db.CompanyProductBenefits.InsertOnSubmit(cp2);
            db.SubmitChanges();
            return RedirectToAction("CoverageCProductIDMN", new { ID = model.CProductId });
        }
        public ActionResult getbenefitsjsonbyCoverage(int ID)
        {
            var benefits = from m in db.BenefitLangs where m.Benefit.CoverageID == ID && m.LangID == Request.Cookies["_culture"]["lang"] 
                           select new { m.DisplayName,m.BenefitID};
            return Json(benefits, JsonRequestBehavior.AllowGet);

        }

        public ActionResult benefitCPremove(int BenefitID,int CompanyProductID)
        {
            var bene = db.CompanyProductBenefits.Where(m => m.BenefitID == BenefitID && m.CProductId == CompanyProductID);
            db.CompanyProductBenefits.DeleteAllOnSubmit(bene);
            db.SubmitChanges();
            return Json(new { success=true},JsonRequestBehavior.AllowGet);
        }
        public ActionResult PackageCompanyProductManager(int ID)
        {
            var productpackage = db.PackageCompanyProducts.Where(m => m.CompanyProduct.ProductID == ID);
            ViewBag.productpackage = productpackage;
            return View();
        }
    }
}
